package com.atguigu.lease.web.app.controller.history;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.BrowsingHistory;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.vo.history.HistoryItemVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@Tag(name = "浏览历史管理")
@RequestMapping("/app/history")
public class BrowsingHistoryController {

    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    @Operation(summary = "获取浏览历史")
    @GetMapping("pageItem")
    private Result<IPage<HistoryItemVo>> page(@RequestParam long current, @RequestParam long size, HttpServletRequest request) {
        IPage<HistoryItemVo> page=new Page<>(current,size);
        String token =request.getHeader("access_token");
        browsingHistoryService.pageInfoList(page,token);
        return Result.ok(page);
    }
    @Operation(summary = "保存或更新浏览历史")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Long room_id, HttpServletRequest request) {
        String token =request.getHeader("access_token");
        Long userId = JwtUtil.parseToken(token).get("userId", Long.class);
        browsingHistoryService.saveOrUpdate(BrowsingHistory.builder().userId(userId).roomId(room_id).browseTime(new Date()).build());
        return Result.ok();
    }
}
